//
//  ViewController.m
//  14-UICollectionView
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 1.UICollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1.itemSize 默认大小 [50, 50]
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    // 2.scrollDirection 滚动方向
    // UICollectionViewScrollDirectionVertical 水平滚动
    // UICollectionViewScrollDirectionHorizontal 上下
    // 滚动方向改变, 会交换列和行(包括区)
    // layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 3.minimumLineSpacing 最小行间距
    layout.minimumLineSpacing = 5;
    // 4.minimumInteritemSpacing 最小列间距
    layout.minimumInteritemSpacing = 5;
    // 5.sectionInset 边距
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 6 headerReferenceSize 区头大小 宽横向滚动时, 设置宽; 纵向滚动时, 设置高
    layout.headerReferenceSize = CGSizeMake(100, 100);
    // 7.footerReferenceSize 区尾大小 宽横向滚动时, 设置宽; 纵向滚动时, 设置高
    layout.footerReferenceSize = CGSizeMake(200, 200);
    // 8. 区头悬浮效果 iOS9.0之后
    layout.sectionHeadersPinToVisibleBounds = YES;
    // 9.区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    // UICollectionView创建
    // 参数1.frame
    // 参数2.填入不能为空的layout(布局)
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    // 只能用注册
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
 }
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor yellowColor];
        return reusableView;
    } else {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor blueColor];
        return reusableView;
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell  =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
